<?php


namespace App\library\services\lib\tencentCloud;

use App\library\services\ConfigService;

class CosLive
{
    /**
     * Author:Zhongyuan Lu
     * @param $streamName
     * @param string $time  过期时间
     * @param int $type 类型 1 推流  2 播流
     * @return string
     */
    public function getUrl($streamName, $time, $type = 1)
    {
        $time = date('Y-m-d H:i:s', $time);
        //推流播流域名
        if ($type == 1) {
            $domain = ConfigService::getValue(config('setting.tencent.domain.push'));
        }elseif ($type == 2) {
            $domain = ConfigService::getValue(config('setting.tencent.domain.pull'));
        }
        //鉴权
        $key = ConfigService::getValue(config('setting.tencent.key'));
        if ($key && $time) {
            $txTime = strtoupper(base_convert(strtotime($time), 10, 16));
            $txSecret = md5($key . $streamName . $txTime);
            $ext_str = "?" . http_build_query(array(
                    "txSecret" => $txSecret,
                    "txTime" => $txTime
                ));
        }
        $prefix = $suffix = '';
        $ext_str = isset($ext_str) ? $ext_str : "";
        //type 1 推流   2 播流
        if ($type == 1) {
            $prefix = 'rtmp://';
        }elseif ($type == 2) {
            $prefix = 'https://';
            $suffix = '.m3u8';
        }
        return "{$prefix}{$domain}/live/{$streamName}{$suffix}{$ext_str}";
    }
}
